package org.jftstool.search.server;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.jftstool.JFTSFramework;
import org.jftstool.config.FieldConfig;
import org.jftstool.config.exceptions.JFTSConfigException;
import org.jftstool.exceptions.JFTSFrameworkException;
import org.jftstool.result.IJFTSResultItem;
import org.jftstool.result.JFTSResult;
import org.jftstool.search.client.JFTSService;
import org.jftstool.search.client.Node;
import org.jftstool.search.client.ResultadoItem;
import org.jftstool.search.client.TipoConsulta.ConsultaPor;
import org.jftstool.searcher.exceptions.JFTSSearcherException;
import org.jftstool.semantic.JFTSHierarchy;
import org.jftstool.semantic.JFTSHierarchyNode;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class JFTSServiceImpl  extends RemoteServiceServlet implements JFTSService {
	private static final long serialVersionUID = 1L;

	@Override
	public Node getOntologyHierarchy() {
		Node root = new Node("root", "", "");
		JFTSFramework jfts = JFTSFramework.getJFTSInstance();
		try {
			jfts.startSession();
			JFTSHierarchy ontology = jfts.getOntoCategorization();
			
			List<Node> nodes = new ArrayList<Node>();
			
			JFTSHierarchyNode aux = ontology.getRoot();
			Iterator<JFTSHierarchyNode> it = aux.listSubNodes();
			while(it.hasNext()){
				JFTSHierarchyNode auxNode = it.next();
				Node n = getNode(auxNode);
				
				if (n != null){
					nodes.add(n);
				}
			}
			root.setChildrens(nodes);

		}
		catch (JFTSFrameworkException e) {
			e.printStackTrace();
		}
		catch (JFTSSearcherException e) {
			e.printStackTrace();
		}
		
		return root;
	}
	
	private Node getNode(JFTSHierarchyNode ontoNode){
		String xxx = ontoNode.getPath();		
		Node n = new Node(ontoNode.getId(), ontoNode.getPath(), new Integer(ontoNode.getAmount()).toString());
		
		Iterator<JFTSHierarchyNode> it = ontoNode.listSubNodes();
		
		if (it.hasNext()){
			List<Node> nodes = new ArrayList<Node>();
			
			while(it.hasNext()){
				JFTSHierarchyNode aux = it.next();
				Node node = getNode(aux);
				nodes.add(node);			
			}
			n.setChildrens(nodes);
		}
		else{
			n.setChildrens(new ArrayList<Node>());
		}
		return n;
	}


	@Override
	public List<ResultadoItem> executeQuery(String query,
			ConsultaPor tipoConsulta, String language, String field) {
		List<ResultadoItem> documentos = new ArrayList<ResultadoItem>();		
		JFTSFramework jfts = JFTSFramework.getJFTSInstance();
		JFTSResult result = null;		
		try {
			switch (tipoConsulta){
			case COMODINES:
				result = jfts.wildCardQuerySearch(field, query, null, 1, 1000);								
				break;
			case FRASE:
				result = jfts.phraseQuerySearch(field, query, null, 1, null, 1, 1000);				
				break;
			case PALABRA:
				result = jfts.simpleQuerySearch(field, query, null, 1, 1000);				
				break;
			case PARSER:
				result = jfts.humanQuerySearch(query, null, 1, 1000, language);				
				break;
			case PREFIJO:
				result = jfts.prefixQuerySearch(field, query, null, 1, 1000);
			}
			
			//Close session
		//	jfts.closeSession();
			
			Iterator<IJFTSResultItem> it = result.getDocuementsIterator();
			
			while(it.hasNext()){
				IJFTSResultItem aux = it.next();
				ResultadoItem resultadoItem = new ResultadoItem(aux.getId(), aux.getCategory());
				documentos.add(resultadoItem);
			}
		}
		catch (JFTSFrameworkException e) {
			e.printStackTrace();
		}
		catch (JFTSSearcherException e) {
			e.printStackTrace();
		}
		return documentos;		
	}

	@Override
	public List<String> getValidFields() {
		JFTSFramework jfts = JFTSFramework.getJFTSInstance();
		List<String> result = null;
		try {
			
			List<FieldConfig> aux = jfts.getJFTSConfiguration().getDocumentConfig().getValidFields();
			if (aux != null){
				result = new ArrayList<String>();
				for (FieldConfig f : aux){
					result.add(f.getArg("name"));
				}
			}
		}
		catch (JFTSConfigException e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public List<String> getValidLanguages() {
		JFTSFramework jfts = JFTSFramework.getJFTSInstance();
		List<String> result = null;
		try {
			//Start session
		//	jfts.startSession();
			
			String [] aux = jfts.getJFTSConfiguration().getSupportedLanguages();
			result = Arrays.asList(aux);
			//Close session
//			jfts.closeSession();
		}
		catch (JFTSConfigException e) {
			e.printStackTrace();
		}

		return result;
	}
}